package com.distributeredis.redis_springboot.springapachehttpclienthttpconnectionresttemplate.resttemplate.controller;

import com.distributeredis.redis_springboot.springapachehttpclienthttpconnectionresttemplate.resttemplate.vo.User;
import com.distributeredis.redis_springboot.springapachehttpclienthttpconnectionresttemplate.resttemplate.vo.api.TianXingResult;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * restTemplate的getForObject()和getForEntity()方式区别是，getForEntity()返回的有封装的状态码
 *
 */
@NoArgsConstructor
@Data
@RestController
@RequestMapping("/rest")
public class RestTemplateController {
    @Autowired
    private RestTemplate restTemplate;

    /**
     * get调用 方式一
     * @param user
     * @return
     */
    @GetMapping("/test0")
    public ResponseEntity test0(User user) {
        String userName = user.getUserName();
        String age = user.getAge();
        String url = "http://localhost:8080/getpost/get?userName="+userName+"&age="+age;
        User userData = restTemplate.getForObject(url, User.class);
        return  ResponseEntity.ok(userData);
    }

    /**
     * get调用 方式二
     * @param user
     * @return
     */
    @GetMapping("/test1")
    public ResponseEntity test1(User user) {
        String url = "http://localhost:8080/getpost/get?userName={1}&age={2}";
        User userData = restTemplate.getForObject(url, User.class,user.getUserName(),user.getAge());
        return  ResponseEntity.ok(userData);
    }

    /**
     * post调用
     * @param user
     * @return
     */
    @GetMapping("/test2")
    public ResponseEntity test3(User user) {
        String url = "http://localhost:8080/getpost/post";
        HttpEntity<User> httpEntity = new HttpEntity<>(user);
        User userData = restTemplate.postForObject(url, httpEntity,User.class);
        return  ResponseEntity.ok(userData);
    }

    /**
     * exchange使用
     * @param user
     * @return
     */
    @GetMapping("/test5")
    public ResponseEntity test5(User user) {
        String url = "http://localhost:8080/getpost/post";
        HttpEntity<User> httpEntity = new HttpEntity<>(user);
        ResponseEntity<User> entity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, User.class);
        User body = entity.getBody();
        return  ResponseEntity.ok(body);
    }

    // 调用天行api
    @GetMapping("/test6")
    public ResponseEntity test6(User user) {

//        String url = "https://apis.tianapi.com/tiangou/index?key="+"4556c57375b2fcda12f6fc50f8570558";
        String url = "https://apis.tianapi.com/tiangou/index?key={1}";
        // 方式一
//        TianXingResult xingResult = restTemplate.getForObject(url, TianXingResult.class);
        // 方式二
//        String url = "https://apis.tianapi.com/tiangou/index?key={1}";
//        TianXingResult xingResult = restTemplate.getForObject(url, TianXingResult.class, "4556c57375b2fcda12f6fc50f8570558");
        // 方式三
        ResponseEntity<TianXingResult> exchange = restTemplate.exchange(url, HttpMethod.GET, null, TianXingResult.class,"4556c57375b2fcda12f6fc50f8570558");
        TianXingResult body = exchange.getBody();
        System.out.println(body);
        return ResponseEntity.ok(body);
    }

    /**
     * 带请求头
     * @param
     * @return
     */
    @GetMapping("/test3")
    public ResponseEntity test4() {
        String url = "https://www.bilibili.com/movie/?spm_id_from={1}";
        Object userData = restTemplate.getForObject(url,Object.class,"333.1007.0.0");
        return  ResponseEntity.ok(userData);
    }
}
